/**=========================================================
 * Controller: SystemCommonQueryUrlController
  =========================================================*/
App.controller('SystemCommonQueryUrlController', ['$scope', 'toaster', 'ngDialog', 'Utils', 'HttpUtils', 'DialogUtils', 'SimpleUiGrid',
    function($scope, toaster, ngDialog, utils, httpUtils, dialogUtils, simpleUiGridService) {
        //===========================commonQueryUrlGrid=================================            
        var commonQueryUrlGridColumns = [
            {
                displayName: 'URL',
                field: 'url'
            },
            {
                displayName: '描述',
                field: 'description'
            },
            { 
                name: '', 
                field: 'widgets', 
                width: '150',
                enableCellEdit: false,
                cellTemplate: '\
                    <div class="ui-grid-cell-contents" style="text-align:center;">\
                        <button ng-click="grid.appScope.saveCommonQueryUrl(row.entity)" title="保存" class="btn btn-xs btn-info">\
                            <em class="fa fa-save"></em>\
                        </button>\
                        <button ng-click="grid.appScope.deleteCommonQueryUrl(row.entity)" title="删除" class="btn btn-xs btn-danger">\
                            <em class="fa fa-trash"></em>\
                        </button>\
                    </div>\
                '
            }
        ];
        
        $scope.searchCommonQueryUrlGrid = function() {
            commonQueryUrlGrid.draw({namespace: 'SystemMapper.getCommonQueryUrls'});
        }; 

        var commonQueryUrlGrid = simpleUiGridService.init($scope, 
            {
                optionModel: 'commonQueryUrlGridOptions',
                pageOptionModel: 'commonQueryUrlGridPageOptions',
                gridApi: 'commonQueryUrlGridApi',
                columns: commonQueryUrlGridColumns,
                pageType: 'common',
                needExport: false,
                menus: [
                    {
                        title: '新增',
                        action: function ($event) {
                            commonQueryUrlGrid.addRow({},0);
                        }
                    }
                ]
            }
        );

        $scope.saveCommonQueryUrl = function(data){
            httpUtils.post('/sysCommonQueryUrl/save',data,
                function(result){
                    if (result.success){
                        if (utils.isEmpty(data.tid)){
                            data.tid = result.msg;
                        }
                        toaster.pop('success', '', '保存成功');
                    }else{
                        toaster.pop('error', '', result.msg);
                    }
                },
                function(){
                    toaster.pop('error', '', '保存失败');
                }
            );
        };
        
        $scope.deleteCommonQueryUrl = function(data){
            dialogUtils.confirm('确定要删除吗?',
                function () {
                    httpUtils.post('/sysCommonQueryUrl/delete',data,
                        function(result){
                            if (result.success){
                                commonQueryUrlGrid.deleteRow(data);
                                toaster.pop('success', '', '删除成功');
                            }else{
                                toaster.pop('error', '', result.msg);
                            }
                        },
                        function(result){
                            toaster.pop('error', '', '删除失败');
                        }
                    );
                }
            );       
        };

        
//===========================init================================= 
        $scope.searchCommonQueryUrlGrid();

    }
]);
